<?php
/**
 * Created by JetBrains PhpStorm.
 * User: DongVan
 * Date: 1/3/14
 * Time: 3:29 PM
 * To change this template use File | Settings | File Templates.
 */

namespace Post_facebook;

date_default_timezone_set("Asia/Ho_Chi_Minh");
class Comments
{
    private $db;
    private $debug;
    public function __construct()
    {
        if (!isset($_SESSION['db']) || empty($_SESSION['db'])) {
            die('lost database connection ...');
        }
        $this->db = $_SESSION['db'];
        $this->debug = $_SESSION['debug'];
    }


    public function insert($arrayComments_facebook, $post_id)
    {
        $id = $arrayComments_facebook['id'];
        $cm_uid = $arrayComments_facebook['from']['id'];
        $cm_uname = $arrayComments_facebook['from']['name'];
        $cm_uimage = 'http://graph.facebook.com/' . $cm_uid . '/picture';
/*        if(isset($arrayComments_facebook['message'])){
            $cm_message = $arrayComments_facebook['message'];
        }else{
            $cm_message = "";
        }*/
        $cm_message = str_replace('\'','',$arrayComments_facebook['message']);
        $like_count = $arrayComments_facebook['like_count'];
        $cm_created = date('Y/m/d h:i:s', strtotime($arrayComments_facebook['created_time']));
        $sql = "INSERT INTO fb_comments SET id = '" . $id . "',
                                                post_id = '" . $post_id . "',
                                                cm_uid = " .$cm_uid . ",
                                                cm_uname = '" . $cm_uname . "',
                                                cm_uimage = '" .$this->db->escape($cm_uimage). "',
                                                cm_message = '" . $this->db->escape($cm_message) . "',
                                                cm_like = " . (int)$like_count . ",
                                                cm_created = '".$cm_created."'
                                                ON DUPLICATE KEY UPDATE
                                                                        cm_message = '" . $this->db->escape($cm_message) . "',
                                                                        cm_like = " . (int)$like_count ;
//        $this->debug->w($sql,1);
        $this->db->query($sql);
    }

    public function getCommentByPostID($data = array(),$post_id)
    {
        $sql = "SELECT * FROM fb_comments where post_id = '".$post_id."'";
        $result = $this->db->query($sql)->rows;
//        $this->debug->w($sql,1);
        return !empty($result) ? $result : array();
    }
}

?>